package cn.stronglink.assetmanage;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import junit.framework.Test;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.stronglink.assetmanage.dao.AssetDao;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.InventoryDetailDao;
import cn.stronglink.assetmanage.dao.RepailDetailDao;
import cn.stronglink.assetmanage.dao.RepairDao;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.entity.Check;
import cn.stronglink.assetmanage.entity.Inventory;
import cn.stronglink.assetmanage.entity.InventoryDetail;
import cn.stronglink.assetmanage.entity.RepailDetail;
import cn.stronglink.assetmanage.entity.Repair;
import cn.stronglink.assetmanage.entity.Result;
import cn.stronglink.assetmanage.index.contract.MainContract;
import cn.stronglink.netrequest.business.base.BaseDataService;
import cn.stronglink.netrequest.business.check.CheckService;
import cn.stronglink.netrequest.business.inventory.InventoryService;
import cn.stronglink.netrequest.business.patrol.PatrolService;
import cn.stronglink.netrequest.business.repail.RepairService;
import cn.stronglink.netrequest.business.search.SearchService;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by Tgb on 2018/6/15.
 */

public class TestActivity extends AppCompatActivity {
    @BindView(R.id.tv_test)
    TextView textView;
    private RepairService repairService;
    private SearchService searchService;
    private PatrolService patrolService;
    private CheckService checeService;
    private InventoryService inventoryService;
    private BaseDataService baseDataService;
    private Handler mHandler = new Handler();
    private Gson gson = new Gson();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        ButterKnife.bind(this);
        checeService = new CheckService();
        repairService = new RepairService();
        searchService = new SearchService();
        patrolService = new PatrolService();
        inventoryService = new InventoryService();
        baseDataService = new BaseDataService();
    }

    public void onClick(View view){
        // 验收
        try {
            checeService.downloadPresetWorkOrder(new Callback() {
                @Override
                public void onFailure(Call call, final IOException e) {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(e.getMessage());
                        }
                    });
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String json = response.body().string();
                    Result<List<Check>> checkResult = null;
                    checkResult = gson.fromJson(json,new TypeToken<Result<List<Check>>>(){}.getType());
                    if(checkResult!=null&&checkResult.getHttpCode()== HttpURLConnection.HTTP_OK){
                        if(checkResult.getData()!=null) {
                            List<Check> checkList = checkResult.getData();
                            // 验收单列表
                            if(checkList!=null&&checkList.size()>0){
                                for(Check check:checkList){
                                    check.setOrderType(1);
                                    DaoManager.getDaoSession(TestActivity.this).getCheckDao().insert(check);
                                    // 获取详情
                                    if(check.getAssetList()!=null&&check.getAssetList().size()>0){
                                        for (Asset asset:check.getAssetList()){
                                            asset.setOrderNo(check.getId_());
                                            DaoManager.getDaoSession(TestActivity.this).getAssetDao().insert(asset);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    // 测试数据库
                    List<Check> checks =  DaoManager.getDaoSession(TestActivity.this).getCheckDao().loadAll();
                    if (checks!=null&&checks.size()>0){
                        for (Check c :checks) {
                            List<Asset> assets =  DaoManager.getDaoSession(TestActivity.this)
                                    .getAssetDao().queryBuilder().where(AssetDao.Properties.OrderNo.eq(c.getId_())).list();
                            if (assets!=null&&assets.size()>0){
                                c.setAssetList(assets);
                            }
                        }
                        // 打印数据
                        Log.i("json","验收数据"+gson.toJson(checks));
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 盘点
        try {
            inventoryService.downloadInventoryWorkOrder("878827689403076608",new Callback() {
                @Override
                public void onFailure(Call call, final IOException e) {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(e.getMessage());
                        }
                    });
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String json = response.body().string();
                    Result<List<Inventory>> inventoryResult = null;
                    inventoryResult = gson.fromJson(json,new TypeToken<Result<List<Inventory>>>(){}.getType());
                    if(inventoryResult!=null&&inventoryResult.getHttpCode()== HttpURLConnection.HTTP_OK){
                        if(inventoryResult.getData()!=null) {
                            List<Inventory> inventoryList = inventoryResult.getData();
                            // 验收单列表
                            if(inventoryList!=null&&inventoryList.size()>0){
                                for(Inventory inventory:inventoryList){
                                    inventory.setOrderType(2);
                                    DaoManager.getDaoSession(TestActivity.this).getInventoryDao().insert(inventory);
                                    // 获取详情
                                    if(inventory.getDetailList()!=null&&inventory.getDetailList().size()>0){
                                        for (InventoryDetail detail:inventory.getDetailList()){
                                            detail.setOrderNo(inventory.getId_());
                                            DaoManager.getDaoSession(TestActivity.this).getInventoryDetailDao().insert(detail);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    // 测试数据库
                    List<Inventory> inventories =  DaoManager.getDaoSession(TestActivity.this).getInventoryDao().loadAll();
                    if (inventories!=null&&inventories.size()>0){
                        for (Inventory c :inventories) {
                            List<InventoryDetail> details =  DaoManager.getDaoSession(TestActivity.this)
                                    .getInventoryDetailDao().queryBuilder().where(InventoryDetailDao.Properties.OrderNo.eq(c.getId_())).list();
                            if (details!=null&&details.size()>0){
                                c.setDetailList(details);
                            }
                        }
                        // 打印数据
                        Log.i("json","盘点数据"+gson.toJson(inventories));
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }


        //        // 维修
        try {
            repairService.downloadRepairWorkOrder("878827689403076608",new Callback() {
                @Override
                public void onFailure(Call call, final IOException e) {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(e.getMessage());
                        }
                    });
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String json = response.body().string();
                    Result<List<Repair>> result = null;
                    result = gson.fromJson(json,new TypeToken<Result<List<Repair>>>(){}.getType());
                    if(result!=null&&result.getHttpCode()== HttpURLConnection.HTTP_OK){
                        if(result.getData()!=null) {
                            List<Repair> repairList = result.getData();
                            // 验收单列表
                            if(repairList!=null&&repairList.size()>0){
                                for(Repair repair:repairList){
                                    repair.setOrderType(3);
                                    DaoManager.getDaoSession(TestActivity.this).getRepairDao().insert(repair);
                                    // 获取详情
                                    if(repair.getDetailList()!=null&&repair.getDetailList().size()>0){
                                        for (RepailDetail detail:repair.getDetailList()){
                                            detail.setRepairId_(repair.getId_());
                                            DaoManager.getDaoSession(TestActivity.this).getRepailDetailDao().insert(detail);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    // 测试数据库
                    List<Repair> repairs =  DaoManager.getDaoSession(TestActivity.this).getRepairDao().loadAll();
                    if (repairs!=null&&repairs.size()>0){
                        for (Repair r :repairs) {
                            List<RepailDetail> details =  DaoManager.getDaoSession(TestActivity.this)
                                    .getRepailDetailDao().queryBuilder().where(RepailDetailDao.Properties.RepairId_.eq(r.getId_())).list();
                            if (details!=null&&details.size()>0){
                                r.setDetailList(details);
                            }
                        }
                        // 打印数据
                        Log.i("json","维修数据"+gson.toJson(repairs));
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }



        // 维修 上传维修单测试  这里应查询已完成的单子列表   我测试直接查询的所有
//        List<Repair> repairList = DaoManager.getInstance(TestActivity.this).getDaoSession().getRepairDao().loadAll();
//        if(repairList!=null&&repairList.size()>0){
//            for (Repair r:repairList) {
//                List<RepailDetail> repailDetailList = DaoManager.getInstance(TestActivity.this).
//                        getDaoSession().getRepailDetailDao().queryBuilder().where(RepailDetailDao.Properties.RepairId_.eq(r.getId_())).list();
//                if(repailDetailList!=null&&repailDetailList.size()>0){
//                    r.setDetailList(repailDetailList);
//                }
//                String json = gson.toJson(r);
//                // 上传这里需要处理一下线程同步问题 等请求成功了在进行下一次循环   上传下一张单子
//                try {
//                    repairService.uploadRepairWorkOrder(json,new Callback() {
//                        @Override
//                        public void onFailure(Call call, final IOException e) {
//                            mHandler.post(new Runnable() {
//                                @Override
//                                public void run() {
//                                    textView.setText(e.getMessage());
//                                }
//                            });
//                        }
//
//                        @Override
//                        public void onResponse(Call call, Response response) throws IOException {
//                            String json = response.body().string();
//                            Log.i("json",json);
//                            mHandler.post(new Runnable() {
//                                @Override
//                                public void run() {
//
//                                }
//                            });
//                        }
//                    });
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }


        // 获取所有资产数据
//        try {
//            searchService.downloadAssetData(new Callback() {
//                @Override
//                public void onFailure(Call call, final IOException e) {
//                    mHandler.post(new Runnable() {
//                        @Override
//                        public void run() {
//                            textView.setText(e.getMessage());
//                        }
//                    });
//                }
//
//                @Override
//                public void onResponse(Call call, Response response) throws IOException {
//                    String json = response.body().string();
//
//                    mHandler.post(new Runnable() {
//                        @Override
//                        public void run() {
//
//                        }
//                    });
//                }
//            });
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
        // 巡检
        try {
            patrolService.downloadPatrolWorkOrder("878827689403076608",new Callback() {
                @Override
                public void onFailure(Call call, final IOException e) {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(e.getMessage());
                        }
                    });
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String json = response.body().string();

                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {

                        }
                    });
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }




        try {
            baseDataService.getBaseData(new Callback() {
                @Override
                public void onFailure(Call call, final IOException e) {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(e.getMessage());
                        }
                    });
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String json = response.body().string();

                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {

                        }
                    });
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
